// ignore_for_file: must_be_immutable
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/network_image_load/image_utils.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/network_image_sll.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/my_gesture_detector.dart';
import 'package:yt_devops/pages/site/site_list/site_list_model.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/enums.dart';

class SiteItemView extends StatelessWidget {
  final SiteModel model;
  final void Function(SiteModel model) onItemTaped;
  final void Function(SiteModel model) favoriteClick;
  const SiteItemView({required this.model, required this.onItemTaped,required this.favoriteClick, super.key});
  ImageProvider _judgeSiteImage() {
    if (model.logo == null || model.logo!.isEmpty) {
      return const AssetImage(ImageAssets.siteDefaultLogo);
    }
    return NetworkImageSSL(imageUrlPath + model.logo!);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        margin:  const EdgeInsets.only(bottom: 12),
        padding: EdgeInsets.only(top: 12,left: 12.w,right: 12.w),
        clipBehavior: Clip.hardEdge,
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(8)),
          boxShadow: [
            BoxShadow(
              color: Colour.borderColor, // 设置阴影颜色
              blurRadius: 6, // 阴影模糊程度
              offset: Offset(0, 3), // 阴影偏移量
            ),
          ],
        ),
        child: Container(
          margin: const EdgeInsets.only(bottom: 6),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: const EdgeInsets.only(bottom: 8),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: LoadImage(
                    model.logo ?? '',
                    height: 80.w,
                    width: 80.w,
                    fit: BoxFit.cover,
                    holderImg: 'assets/images/site_home_default',
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(left: 12.w),
                  padding: const EdgeInsets.only(
                    bottom: 2,
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[

                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            child: Text(
                              model.name!,
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 16.sp,
                                color: Colour.mainTextColor,
                                fontWeight: FontWeight.w500,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          _statusWidget(model.status),
                        ],
                      ),
                   Gaps.vGap4,
                      Row(
                        children: [
                          Expanded(child: Padding(
                            padding: const EdgeInsets.fromLTRB(0, 4, 0, 0),
                            child: Text(
                              model.address!,
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 12.sp,
                                color: Colour.black,
                                fontWeight: FontWeight.w400,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          )),
                         PermissionWidget(permissionStringList: const [PermissionConstant.homeSiteCollection], child: MyGestureDetector(onTap: (){
                           favoriteClick(model);
                         }, child: Container(
                             padding: EdgeInsets.only(right: 6.w,top: 2),
                             child: Icon(model.collectFlag != null && model.collectFlag == 1?Icons.favorite:Icons.favorite_border,
                               color: model.collectFlag != null && model.collectFlag == 1?
                               const Color(0xFFFA5B5B):Colour.otherTextColor, size: 16.sp,)
                         ))),
                        ],
                      ),
                      model.energyOptions.toString() == '23'||model.energyOptions.toString() == '123' ?
                      Container(height: 0,):Gaps.vGap4,
                      Container(
                        // color: Colors.red,
                        child: _getCapacityWidget(),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      onTap: () => onItemTaped(model),
    );
  }

  // 1光伏站点 2儲能站点  、3充电站点，   光储站点，"12"，（”光储充站点”，"123"），（储充站点，23) 光充站点13
  _getCapacityWidget() {
    if (model.energyOptions.toString() == '1') {
      SiteListItem e = SiteListItem(
        ImageAssets.sitePvChargeIcon,
        model.photovoltaicInstalledCapacity?.toString() ?? '0',
        'kWp',
        0,
      );
      return Container(
        margin: const EdgeInsets.only(top:5),
        child: _getItem(e),);
    } else if (model.energyOptions.toString() == '2') {
      SiteListItem e = SiteListItem(
        ImageAssets.siteLoadChargeIcon,
        '${model.energyStoragePower?.toString() ?? '0'}kW/${model.energyStorageCapacity?.toString() ?? '0'}',
        'kWh',
        1,
      );
      return Container(
          margin: EdgeInsets.only(top:5),
          child:_getItem(e, maxWidth: 200));
    } else if (model.energyOptions.toString() == '3') {
      SiteListItem e = SiteListItem(
        ImageAssets.siteEvChargerIcon,
        model.chargingStationCapacity?.toString() ?? '0',
        'kW',
        2,
      );
      return Container(
          margin: const EdgeInsets.only(top:5),
          child:_getItem(e, maxWidth: 200));
    } else if (model.energyOptions.toString() == '12') {
      SiteListItem e1 = SiteListItem(
        ImageAssets.sitePvChargeIcon,
        model.photovoltaicInstalledCapacity?.toString() ?? '0',
        'kWp',
        0,
      );
      SiteListItem e2 = SiteListItem(
        ImageAssets.siteLoadChargeIcon,
        '${model.energyStoragePower?.toString() ?? '0'}kW/${model.energyStorageCapacity?.toString() ?? '0'}',
        'kWh',
        1,
      );
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _getItem(e1),
          _getItem(e2, maxWidth: 200),
        ],
      );
    } else if (model.energyOptions.toString() == '13') {
      SiteListItem e1 = SiteListItem(
        ImageAssets.sitePvChargeIcon,
        model.photovoltaicInstalledCapacity?.toString() ?? '0',
        'kWp',
        0,
      );
      SiteListItem e2 = SiteListItem(
        ImageAssets.siteLoadChargeIcon,
        model.chargingStationCapacity?.toString() ?? '0',
        'kW',
        2,
      );
      return Container(
          margin: EdgeInsets.only(top:5),
          child:Wrap(
        children: [
          _getItem(e1),
          _getItem(e2,),
        ],
      ));
    } else if (model.energyOptions.toString() == '23') {
      SiteListItem e2 = SiteListItem(
        ImageAssets.siteLoadChargeIcon,
        '${model.energyStoragePower?.toString() ?? '0'}kW/${model.energyStorageCapacity?.toString() ?? '0'}',
        'kWh',
        2,
      );
      SiteListItem e1 = SiteListItem(
        ImageAssets.siteEvChargerIcon,
        model.chargingStationCapacity?.toString() ?? '0',
        'kW',
        1,
      );

      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _getItem(e1),
          _getItem(e2, maxWidth: 200),
        ],
      );
    } else {
      SiteListItem e1 = SiteListItem(
        ImageAssets.sitePvChargeIcon,
        model.photovoltaicInstalledCapacity?.toString() ?? '0',
        'kWp',
        0,
      );
      SiteListItem e2 = SiteListItem(
        ImageAssets.siteEvChargerIcon,
        model.chargingStationCapacity?.toString() ?? '0',
        'kW',
        1,
      );
      SiteListItem e3 = SiteListItem(
        ImageAssets.siteLoadChargeIcon,
        '${model.energyStoragePower?.toStringAsFixed(0) ?? '0'}kW/${model.energyStorageCapacity?.toStringAsFixed(0) ?? '0'}',
        'kWh',
        2,
      );
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Wrap(
            children: [
              _getItem(e1),
              _getItem(e2,),
            ],
          ),
          _getItem(e3, maxWidth: 200),
        ],
      );
    }
  }

  _getItem(e, {double maxWidth = 90}) {
    return ConstrainedBox(
      constraints: BoxConstraints(maxWidth: ScreenUtil().setWidth(maxWidth)),
      child: Container(
        // color: Colors.red,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Image.asset(
              e.label!,
              width: 16.w,
              height: 16,
            ),
            Expanded(
              child: Text(
                '${e.value}${e.unit}',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colour.labelTextColor,
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _statusWidget(SiteStatusType? statusType) {
    Color color;
    switch (statusType) {
      /// 正常
      case SiteStatusType.normal:
        color = const Color(0xFF00AE66);
        break;

      /// 故障
      case SiteStatusType.fault:
        color = const Color(0xFFFA3F3F);
        break;

      /// 断连
      default:
        color = Colour.labelTextColor;
        break;
    }
    return Container(
      // width: 32.w,

      height: 22,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(
          Radius.circular(4.sp),
        ),
        color: color.withOpacity(0.1),
      ),
      child: Padding(
        padding: EdgeInsets.only(left: 3.w,right: 3.w,top: 2),
        child: Text(
          textAlign: TextAlign.center,
          statusType?.label ?? '',
          style: TextStyles.styleWith(12.sp, color: color),
        ),
      ),
    );
  }
}
